'use strict'
import md5 from 'js-md5'

export function getMd5(file) {
  return new Promise(function (resolve, reject) {
    var reader = new FileReader()
    reader.readAsArrayBuffer(file)
    reader.onload = function (e) {
      var buffer = e.target.result
      var hash = md5(buffer)
      resolve(hash)
    }
  })
}

export function getUniqueFileName(originFileName) {
  if (!originFileName) return ''
  let extName = ''
  let dotIndex = originFileName.lastIndexOf('.')
  if (dotIndex > -1) {
    extName = originFileName.substring(dotIndex)
  }

  let randomNum = window.Math.floor(Math.random() * 10000)
  let timespan = window.Date.parse(new window.Date())
  return timespan.toString() + randomNum.toString() + extName
}
